package org.zhadoop.stream.cql.tasks;

import java.util.List;

import org.zhadoop.stream.cql.CQLResult;
import org.zhadoop.stream.cql.DriverContext;
import org.zhadoop.stream.cql.exception.CQLException;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.hooks.SemanticAnalyzeHook;
import org.zhadoop.stream.cql.semanticanalyzer.analyzecontext.AnalyzeContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.ParseContext;
import org.zhadoop.streaming.config.StreamingConfig;

/**
 * 基础任务执行器
 * @author zwd
 *
 */
public abstract class BaseTask implements Task{
	private List<SemanticAnalyzeHook> hooks;
	
	private StreamingConfig conf;
	
	@Override
	public void init(DriverContext driverContext, StreamingConfig config, List<SemanticAnalyzeHook> analyzeHooks)
			throws CQLException {
		this.hooks = analyzeHooks;
		this.conf = config;
	}
	
	@Override
	public abstract void execute(ParseContext parseContext) throws CQLException;
	
	@Override
	public abstract CQLResult getResult();
	
	@Override
	public void preAnalyze(DriverContext cxt, ParseContext parseContext) throws SemanticAnalyzerException {
		for(int i = 0; i < hooks.size(); i++){
			if(hooks.get(i).validate(parseContext)){
				hooks.get(i).preAnalyze(cxt, parseContext);
			}
		}
	}
	
	@Override
	public void postAnalyze(DriverContext context, AnalyzeContext analyzeContext, ParseContext parseContext)
			throws SemanticAnalyzerException {
			for(int i = 0; i < hooks.size(); i++){
				if(hooks.get(i).validate(parseContext)){
					hooks.get(i).postAnalyze(context, analyzeContext);
				}
			}
	}

	public List<SemanticAnalyzeHook> getHooks() {
		return hooks;
	}

	public StreamingConfig getConf() {
		return conf;
	}
}
